Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    title: String,
    required: Boolean,
    limit: {
      type: Number,
      value: 9
    },
    value: {
      type: null,
      value: []
    },
  },
  data: {
    files: []
  },
  methods: {
    chooseImage() {
      const that = this;
      wx.chooseMedia({
        sizeType: ['original', 'compressed'], 
        sourceType: ['album', 'camera'],
        mediaType: ['image'],
        count: this.data.limit,
        success(res) {
          that.setData({
            files: that.data.files.concat(res.tempFiles.map(item => item.tempFilePath)),
          });

          that.triggerEvent('change', that.data.files);
        },
      });
    },
    previewImage(e) {
      wx.previewImage({
        current: e.currentTarget.id, // 当前显示图片的http链接
        urls: this.data.files, // 需要预览的图片http链接列表
      });
    },

    onClear(e) {
      const { index } = e.currentTarget.dataset

      const files = this.data.files;
      files.splice(index, 1);

      this.setData({
        files
      })

      this.triggerEvent('change', this.data.files);
    }
  }
})
